package com.example.myapplication.camera;

import android.content.Context;
import android.graphics.Point;
import android.hardware.camera2.CameraManager;
import android.media.ImageReader;
import android.util.Log;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.View;

/**
 * Create By TreeRoot
 * Create Time: 2024/02/01 08:55
 * Class Name: Builder
 * Description:
 *
 * @author TreeRoot
 */
public final class Builder {
    private TextureView textureView;
    /**
     * 预览显示的view，目前仅支持surfaceView和textureView
     */
    private View previewDisplayView;

    /**
     * 是否镜像显示，只支持textureView
     */
    boolean isMirror;
    /**
     * 事件回调
     */
    private ImageReader.OnImageAvailableListener cameraListener;
    /**
     * 屏幕的长宽，在选择最佳相机比例时用到
     */
    private Point previewViewSize;
    /**
     * 传入getWindowManager().getDefaultDisplay().getRotation()的值即可
     */
    private int rotation;
    /**
     * 额外的旋转角度（用于适配一些定制设备）
     */
    private int additionalRotation;
    /**
     * 相机管理类，用于检查相机是否存在及获取相机相关参数
     */
    private CameraManager cameraManager;
    private Context mContext;

    /**
     * 事件回调
     */
    private CameraListener myCameraListener;

    /**
     * 使用的相机id
     */
    private String cameraId;

    /**
     * 构造函数
     */
    public Builder() {
    }

    public Camera2Helper build() {
        String camera2HelperTag = "camera2HelperTag";
        if (previewViewSize == null) {
            Log.e(camera2HelperTag, "previewViewSize is null, now use default previewSize");
        }
        if (cameraListener == null) {
            Log.e(camera2HelperTag, "cameraListener is null, callback will not be called");
        }
        if (previewDisplayView == null) {
            throw new RuntimeException("you must preview on a textureView or a surfaceView");
        }
        return new Camera2Helper(this);
    }

    public Builder cameraId(String val) {
        cameraId = val;
        return this;
    }

    public Builder previewOn(View val) {
        if (val instanceof SurfaceView || val instanceof TextureView) {
            previewDisplayView = val;
            return this;
        } else {
            throw new RuntimeException("you must preview on a textureView or a surfaceView");
        }
    }

    public Builder myCameraListener(CameraListener val) {
        myCameraListener = val;
        return this;
    }

    public Builder textureView(TextureView val) {
        textureView = val;
        return this;
    }


    public Builder isMirror(boolean val) {
        isMirror = val;
        return this;
    }

    public Builder cameraManager(CameraManager val) {
        cameraManager = val;
        return this;
    }
    public  Builder mContext(Context val){
        mContext = val;
        return this;
    }

    public Builder previewViewSize(Point val) {
        previewViewSize = val;
        return this;
    }

    public Builder rotation(int val) {
        rotation = val;
        return this;
    }

    public Builder cameraListener(ImageReader.OnImageAvailableListener val) {
        cameraListener = val;
        return this;
    }

    public CameraListener getMyCameraListener() {
        return myCameraListener;
    }
    public Context getmContext(){
        return mContext;
    }

    public View getPreviewDisplayView() {
        return previewDisplayView;
    }

    public ImageReader.OnImageAvailableListener getCameraListener() {
        return cameraListener;
    }

    public Point getPreviewViewSize() {
        return previewViewSize;
    }

    public int getRotation() {
        return rotation;
    }

    public int getAdditionalRotation() {
        return additionalRotation;
    }

    public CameraManager getCameraManager() {
        return cameraManager;
    }

    public String getCameraId() {
        return cameraId;
    }

    public TextureView getTextureView() {
        return textureView;
    }

    public boolean isMirror() {
        return isMirror;
    }
}